<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺" prop="storeId">
        <el-select v-model="queryParams.storeId" filterable placeholder="请选择店铺">
          <el-option v-for="item in storeMgmtList" :key="item.id" :label="item.storeAbbr" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="leaderName">
        <el-input placeholder="请输入负责人" v-model="queryParams.leaderName"></el-input>
      </el-form-item>
      <el-form-item label="优化日期" prop="optimizeTime">
        <el-date-picker clearable
                        v-model="queryParams.optimizeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择优化日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="优化状态" prop="optimizeStatus">
        <el-select v-model="queryParams.optimizeStatus" placeholder="请选择优化状态" style="width: 100%;">
          <el-option v-for="dict in dict.type.optimize_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div ref="divTable" :style="'height:' + divHeight + 'px'">
      <el-table v-loading="loading" :data="declineBacklogList" @selection-change="handleSelectionChange">
        <el-table-column label="商品ID" align="center" prop="goodsId" width="120"/>
        <el-table-column label="商品图片" align="center" prop="imgUrl" width="120">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品标签" align="center" prop="goodsLabel" width="110"/>
        <el-table-column label="支付金额" align="center" prop="payAmount"/>
        <el-table-column label="支付金额增降" align="center" prop="amountCrease" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.amountCrease }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="支付买家数" align="center" prop="payBuyerNumber" width="120"/>
        <el-table-column label="支付买家数增降" align="center" prop="payBuyerCrease" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.payBuyerCrease }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="商品访客数" align="center" prop="goodsVisitorNumber" width="120"/>
        <el-table-column label="商品访客数增降" align="center" prop="goodsVisitorCrease" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.goodsVisitorCrease }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="商品转化率" align="center" prop="goodsPayConversionRatio" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.goodsPayConversionRatio }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="推广占比" align="center" prop="promotionRatio" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.promotionRatio }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="推广投产比" align="center" prop="promotionRoi" width="120"/>
        <el-table-column label="行动点" align="center" prop="actionPoint" width="120"/>
        <el-table-column label="待办事项生成时间" align="center" prop="createTime" width="180"/>
        <el-table-column label="负责人" align="center" prop="leaderName" width="100"/>
        <el-table-column label="优化动作" align="center" prop="optimizeActions" width="120"/>
        <el-table-column label="优化时间" align="center" prop="optimizeTime" width="180"/>
        <el-table-column label="优化时长(h)" align="center" prop="optimizationDuration"/>
        <el-table-column label="优化状态" align="center" prop="optimizeStatus">
          <template v-slot="scope">
            <el-tag type="warning" v-if="scope.row.optimizeStatus===0">待优化</el-tag>
            <el-tag type="success" v-if="scope.row.optimizeStatus===1">已优化</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >优化动作
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :pageRefs="this.$refs"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改下滑预警待办事项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="优化动作" prop="optimizeActions">
        <el-select
          v-model="form.optimizeActions"
          multiple
          filterable
          allow-create
          default-first-option
          placeholder="请选择优化动作">
          <el-option
            v-for="item in restaurants"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editOptimizeActions">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addDeclineBacklog,
  delDeclineBacklog,
  editOptimizeActions,
  exportExcel,
  getDeclineBacklog,
  listDeclineBacklog,
  updateDeclineBacklog
} from "@/api/business/declineBacklog";
import ResizeMixin from "@/layout/mixin/ResizeHandler";
import {getNormalStoreMgmtList} from "@/api/business/storeMgmt";
import {parseTime} from "../../../utils/basic";
import {getAllList} from "@/api/business/optimizeActions";

export default {
  name: "DeclineBacklog",
  mixins: [ResizeMixin],
  dicts: ['optimize_status'],
  data() {
    return {
      // div高度
      divHeight: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 下滑预警待办事项表格数据
      declineBacklogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        goodsId: null,
        goodsLabel: null,
        payAmount: null,
        amountCrease: null,
        payBuyerNumber: null,
        payBuyerCrease: null,
        goodsVisitorNumber: null,
        goodsVisitorCrease: null,
        goodsPayConversionRatio: null,
        promotionRatio: null,
        promotionRoi: null,
        actionPoint: null,
        leaderId: null,
        leaderName: null,
        optimizeActions: null,
        optimizeTime: null,
        optimizationDuration: null,
        optimizeResult: null,
        optimizeStatus: null,
        status: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //店铺集合
      storeMgmtList: [],
      //优化动作原始数据
      originalList: [],
      //优化动作参考内容
      restaurants: [],
    };
  },
  created() {
    this.getList();
    this.getNormalStoreMgmtList();
  },
  methods: {
    parseTime,
    /** 查询下滑预警待办事项列表 */
    getList() {
      this.loading = true;
      listDeclineBacklog(this.queryParams).then(response => {
        this.declineBacklogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询店铺列表
    getNormalStoreMgmtList() {
      getNormalStoreMgmtList().then(res => {
        this.storeMgmtList = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        goodsId: null,
        goodsLabel: null,
        payAmount: null,
        amountCrease: null,
        payBuyerNumber: null,
        payBuyerCrease: null,
        goodsVisitorNumber: null,
        goodsVisitorCrease: null,
        goodsPayConversionRatio: null,
        promotionRatio: null,
        promotionRoi: null,
        actionPoint: null,
        leaderId: null,
        leaderName: null,
        optimizeActions: null,
        optimizeTime: null,
        optimizationDuration: null,
        optimizeResult: null,
        optimizeStatus: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.$resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加下滑预警待办事项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeclineBacklog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "填写优化动作";
      });
      //获取优化动作参考数据
      this.loadAll();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeclineBacklog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeclineBacklog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //填写优化动作
    editOptimizeActions(){
      this.form.optimizeActions=this.form.optimizeActions.join(",")
      editOptimizeActions(this.form).then(response => {
        this.$modal.msgSuccess("填写成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除下滑预警待办事项编号为"' + ids + '"的数据项？').then(function () {
        return delDeclineBacklog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      exportExcel(this.queryParams)
    },
    //优化动作参考数据
    loadAll() {
      this.originalList = []
      this.restaurants = []
      getAllList().then(response => {
        this.originalList = response.data
        this.originalList.map(item => {
          this.restaurants.push({
            "value": item.direction.concat("-", item.actionName),
            "label": item.direction.concat("-", item.actionName)
          })
        })
      })
    }
  }
};
</script>
